package com.erp.itm.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.erp.common.utils.ShiroUtils;
import com.erp.fin.domain.PricetypeDO;
import com.erp.fin.domain.PricetypeDTO;
import com.erp.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.itm.domain.PaytypeDO;
import com.erp.itm.service.PaytypeService;
import com.erp.common.utils.PageUtils;
import com.erp.common.utils.Query;
import com.erp.common.utils.R;

/**
 * 付款方式管理
 * 
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-07-12 18:37:12
 */
 
@Controller
@RequestMapping("/itm/paytype")
public class PaytypeController {
	@Autowired
	private PaytypeService paytypeService;
	
	@GetMapping()
	@RequiresPermissions("itm:paytype:paytype")
	String Paytype(){
	    return "itm/paytype/paytype";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("itm:paytype:paytype")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<PaytypeDO> paytypeList = paytypeService.list(query);
		int total = paytypeService.count(query);
		PageUtils pageUtils = new PageUtils(paytypeList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("itm:paytype:add")
	String add(){
	    return "itm/paytype/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("itm:paytype:edit")
	String edit(@PathVariable("id") Long id,Model model){
		PaytypeDO paytype = paytypeService.get(id);
		model.addAttribute("paytype", paytype);
	    return "itm/paytype/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("itm:paytype:add")
	public R save( PaytypeDO paytype){
		UserDO current = ShiroUtils.getUser();
		paytype.setUserIdCreate(current.getUserId());
		paytype.setUserNameCreate(current.getName());
		paytype.setGmtCreate(new Date());
		paytype.setUserIdModified(current.getUserId());
		paytype.setUserNameModified(current.getName());
		paytype.setGmtModified(new Date());
		if(paytypeService.save(paytype)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("itm:paytype:edit")
	public R update( PaytypeDO paytype){
		UserDO current = ShiroUtils.getUser();
		paytype.setUserIdModified(current.getUserId());
		paytype.setUserNameModified(current.getName());
		paytype.setGmtModified(new Date());
		paytypeService.update(paytype);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("itm:paytype:remove")
	public R remove( Long id){
		if(paytypeService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("itm:paytype:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		paytypeService.batchRemove(ids);
		return R.ok();
	}

	@ResponseBody
	@GetMapping("/paytypelist")
	public List<PaytypeDO> paytypelist(){
		//查询列表数据
		List<PaytypeDO> paytypeList = paytypeService.list(null);
		return paytypeList;
	}


}
